package com.zhongwang.business.module.basic.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.common.ValidResult;
import com.zhongwang.business.module.basic.model.DeptModel;
import com.zhongwang.business.module.basic.service.DeptBasicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeptController extends BaseController {


    @Autowired
    private DeptBasicService deptBasicService;


    /**
     * 首页跳转至部门管理页面
     * @param model
     * @return
     */
    @GetMapping("/basic/dept")
    @RequiresPermissions({"basic:dept:view"})
    public String deptPage(Model model) {
        return "/basicManage/dept/page";
    }


    /**
     * 获取列表
     * @return
     */
    @ResponseBody
    @PostMapping("/deptList")
    public List<DeptModel> selectAllInvoiceList(String name) {
        return deptBasicService.selectList(name);
    }


    /**
     * 部门名称唯一验证
     */
    @ResponseBody
    @PostMapping("/deptCheckUnique")
    public ValidResult checkUnique(DeptModel dept) {
        DeptModel m = deptBasicService.findDeptByName(dept.getName());
        if (dept.getId() == null) {
            return ValidResult.result(m);
        }
        if (m == null || dept.getId().equals(m.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建部门
     */
    @ResponseBody
    @PostMapping("/deptSave")
    public JsonResult save(DeptModel dept) {
        return isNotNull(deptBasicService.saveDept(dept));
    }

    /**
     * 修改部门
     */
    @ResponseBody
    @PostMapping("/deptUpdate")
    public JsonResult update(DeptModel dept) {
        return isNotNull(deptBasicService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @ResponseBody
    @PostMapping("/deptDel")
    public JsonResult del(Integer id) {
        return jsonResult(deptBasicService.delDept(id));
    }


}
